package risk.control.application.utils.data;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;

import java.util.concurrent.TimeUnit;


/**
 * 临时缓存
 */
public class TempCacheUtils {

    public static final String ID_CAR_PIC_CACHE_KEY = "id_car_pic_cache_key";

    private static TempCacheUtils instance;
    private static Cache<String, Object> cache;

    // 静态内部类持有单例实例
    private static class Holder {
        private static final TempCacheUtils INSTANCE = new TempCacheUtils();
    }

    // 私有构造器
    private TempCacheUtils() {
        cache = CacheBuilder.newBuilder().maximumSize(100)        // 设置缓存的最大容量为10
                .expireAfterWrite(30, TimeUnit.SECONDS) // 设置缓存项在写入过期
                .build();
    }

    // 静态内部类持有单例实例
    public static TempCacheUtils getInstance() {
        return TempCacheUtils.Holder.INSTANCE;
    }

    public void put(String key, Object value) {
        cache.put(key, value);
    }

    public void remove(String key) {
        cache.invalidate(key);
    }

    public Object get(String key) {
        return cache.getIfPresent(key);
    }

    // 完全清理缓存（退出登录或应用退出时调用）
    public void clearAll() {
        cache.invalidateAll();
    }


}